<?php
namespace Admin\Controller;
use Think\Controller;
class PowerController extends BaseController {
	public function index(){
		$this->display();
	}


	/*
		加载管理权限页面
		roleid 前台ajax传入的值	
	*/
	public function dopower(){
		// 查询出所有权限
		$pri = M('privilege');
		$parent['parentid'] = array('ELT','1');
		$list = $pri->where($parent)->select();
		// dump($list); 32个数据
		$arr = array();
		foreach ($list as $key => $value)
		{
			// dump($value['id']);
			$res = $pri->where("parentid=%d",$value['id'])->select();
			foreach ($res as $k => $v) {
				if($v['parentid'] == $value['id'])
				{
					$list[$key]['aaa'][] = $v;
				}
			}
		}

		// 查询出当前点击的人拥有的权限
		$role = M('role_pri');
		$where['roleid'] = I('get.roleid');
		$ro = $role->field('priid')->where($where)->select();
		foreach ($ro as $key => $value) {
			foreach ($value as $k => $v) {
				$aa[] = $v;
			}
		}
		// dump($aa);	
		// dump($list);

		 
		$this->assign(array(
			'pri'=>$list,
			'aa'=>$aa,
			'roleid'=>I('get.roleid'),
			));
		$this->display();
	}
	/*
		执行修改操作
	*/

	public function editpower(){
		$pri = array_merge(array_unique(I('post.pri')));
		$roleid = I('post.roleid');
		// dump($pri);die;
		// dump($roleid);
		$Power = M('role_pri');
		$Power->startTrans();
		$where['roleid'] = $roleid;
		$res = $Power->where($where)->select();
		if($res){
			$delPores = $Power->where($where)->delete();
		}

		for ($i=0; $i <count($pri) ; $i++) { 
			$data[] = array('priid'=>''.$pri[$i].'','roleid'=>''.$roleid.'');
			
		}

		$addPores = $Power->addAll($data);

		if($addPores){
			$Power->commit();
			$this->success('修改成功','index');
		}else{
			$Power->rollback();
		}
		
	}

}